package Day015_File类_递归_IO流.demo04_递归练习;

/**
 * @author zhangyan
 * @date 2020/5/6 10:49
 */
public class Test1_递归求累加和 {
    public static void main(String[] args) {

      /*
        5的累加和 = 5+4+3+2+1
        4的累加和 = 4+3+2+1
        3的累加和 = 3+2+1
        ...
      */
        int n = 5;
        System.out.println(getSum(n));
    }

    //计算累加和
    public static int getSum(int n) {
        if (n == 1) { //出口
            return 1;
        }
        return n + getSum(n - 1); //规律: n的累加和 = n + (n-1的累加和)...
    }
}
